<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of shippingcom_model
 *
 * @author hassanali
 */
class trades_model extends cms_model{
    //put your code here
    public function __construct() {
        parent::__construct();
        $this->table = 'trades';
        $this->select = array('trades.* ', 'trades_language.title', 'trades_language.lang');
        $this->join = array('trades_language' => 'trades_language.trade_id = trades.id');
        $this->where = array('lang' => LANG);
    }
    
    function aftersave($id = false) {
        if ($id) {
            $dd = $this->data;
            if ($id === true)
                $id = $dd['trade_id'];
            $language = get_languages();
            $this->db->where(array('trade_id' => $id))->delete('trades_language');
            foreach ($language as $la) {
                if (isset($dd["title_$la->code"])) {
                    $this->db->set('trade_id', $id);
                    $this->db->set('lang', $la->code);
                    $this->db->set('title', $dd["title_$la->code"]);
                    $this->db->insert('trades_language');
                }
            }
            return TRUE;
        } else {
            return FALSE;
        }
    }

    function beforedelete($conditions = array(), $truncate = FALSE) {
        parent::beforedelete();
        if ($truncate) {
            $this->db->truncate('trades_language');
        } elseif (count($conditions) > 0) {
            foreach ($conditions as $k => $v)
                $this->db->where_in($k, $v);
            $dd = $this->db->get($this->table)->result();
            $values = array();
            $key = 'trade_id';
            foreach ($dd as $d) {
                $values[] = $d->id;
            }
            if ($values) {
                $this->db->where_in($key, $values);
                $this->db->delete('trades_language');
            }
            else
                return FALSE;
        }else {
            return FALSE;
        }
    }

    function getlanguage($id = false) {
        if ($id) {
            $lan = $this->db->get_where('trades_language', array('trade_id' => $id))->result();
            $return = array();
            foreach ($lan as $l) {
                $return[$l->lang] = $l->title;
            }
            return $return;
        } else {
            return FALSE;
        }
    }
}

?>
